# -*- coding: utf-8 -*-

import time

from PageClass.boe_EmployeePrepaymentClass.Class_EmployeePrepaymentBoe import Class_EmployeePrepaymentBoe
from PageClass.boe_EmployeeLoanClass.Class_EmployeeLoanBoe import Class_EmployeeLoanBoe
from TestCases.common import LoginDepend
from TestCases.common._baseClass import _BaseClass
from Util import config, logger, record


class BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('ecsHost', 'user')
        self.EmployeePrepayment = Class_EmployeePrepaymentBoe(self.login.driver)

    def __exit__(self, *args):
        # pass
        if self.isExit:
            self.exit()

    def exit(self):
        self.EmployeePrepayment.driver.quit()

    def submit(self, boe_data):
        # 打开费报
        logger.info(f"费报地址为：{config.urlDict.url.ecsHost}")  # .\Config\test\UrlConfig.yml
        # 打开员工还款单
        self.EmployeePrepayment.open_boe(boe_data.areaType, boe_data.boeType, boe_data.operationType)
        time.sleep(3)
        # 勾选首行借款记录核销 # 提交
        self.EmployeePrepayment.click_line1()

        # 获取单号写入.\Data\localMemory\record.json
        boeNo = self.EmployeePrepayment.getBoeNum()
        record.writeDataToRecord({'boeNo': boeNo})

        # 还款明细区-还款方式
        self.EmployeePrepayment.select_operationSubType(boe_data.operationSmallType)

        # 还款明细区-还款金额
        self.EmployeePrepayment.input_Money(boe_data.prepayment)

        # 还款明细区-收款账号
        try:
            self.EmployeePrepayment.click_account()
            self.EmployeePrepayment.selectValueFromSmallTable()  # **{'账号名称': boe_data.account}
        except:
            # 非必填字段，暂时放过
            pass

        # 还款明细区-还款日期:今天
        self.EmployeePrepayment.select_loanRepaymentDate()

        # 还款明细区-还款说明
        self.EmployeePrepayment.input_loanremark(boe_data.loanremark)

        # 核销明细区-本次核销金额
        self.EmployeePrepayment.input_cavAmount(boe_data.prepayment)
        time.sleep(3)
        # 保存单据
        # with allure.step("保存单据"):
        #     self.EmployeePrepayment.click_boeSaveButton()
        #     time.sleep(5)
        # 提交单据
        message = self.EmployeePrepayment.click_boeSubmitButton()
        return message


class EmployeeLoan_BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('ecsHost', 'user')
        self.EmployeeLoan = Class_EmployeeLoanBoe(self.login.driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self.EmployeeLoan.driver.quit()

    def submit(self, boe_data):
        # 打开费报
        logger.info(f"费报地址为：{config.urlDict.url.ecsHost}")  # .\Config\test\UrlConfig.yml

        # 打开员工借款单
        self.EmployeeLoan.open_boe(boe_data.areaType, boe_data.boeType, boe_data.operationType)

        # 获取单号
        boeNo = self.EmployeeLoan.getBoeNum()
        record.writeDataToRecord({'boeNo': boeNo})

        # 主表区：业务类型
        operationType = boe_data.operationType
        self.EmployeeLoan.input_operationType(operationType)

        # 主表区：备注
        boeAbstract = boe_data.boeAbstract
        self.EmployeeLoan.inputboeAbstract(boeAbstract)

        # 借款明细区：业务类型-小类
        TypeId_Name = boe_data.TypeId_Name
        self.EmployeeLoan.SelectType(TypeId_Name)

        # 借款明细区：总金额
        expenseAmount = boe_data.expenseAmount
        self.EmployeeLoan.inputExpenseAmount(expenseAmount)

        # 借款明细区：项目
        Project = boe_data.Project
        self.EmployeeLoan.inputProject()  # Project

        # # 保存单据
        # self.EmployeeLoan.click_boeSaveButton()
        # time.sleep(5)

        # 提交单据
        message = self.EmployeeLoan.click_boeSubmitButton()
        return message
